<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .isNot {
            text-decoration: line-through;
            color: gray;
        }
    </style>
</head>

<body>

    <div id="app">
        <input type="text" v-on:keyup.enter="insert" v-model="newValue">
        <ul>
            <li v-for="item in allFinish">
                <input type="checkbox" v-model="item.isFinish">
                <span :class="item.isFinish?'isNot':''">{{item.name}}</span>
            </li>
        </ul>
        <button @click="Finish = undefined">全部</button>
        <button @click="Finish = true">已完成</button>
        <button @click="Finish = false">未完成</button>
        {{Finish}}
    </div>



    <script src="js/vue.js"></script>
    <script>

        let app = Vue.createApp({
            data() {
                return {
                    newValue: '',
                    Finish: undefined,
                    todos: [
                        { id: 1, name: '喝酒', isFinish: false },
                        { id: 2, name: '抽烟', isFinish: true },
                        { id: 3, name: '烫头', isFinish: false }
                    ]
                }
            },
            methods: {
                // 完成添加操作
                insert() {
                    if (!this.newValue.length) {
                        return;
                    }
                    this.todos.push({ id: 5, name: this.newValue, isFinish: false });
                    this.newValue = '';
                }
            },
            computed: {
                // 筛选完成情况,全部的情况：
                allFinish() {
                    // 筛选全部
                    if (this.Finish == undefined)
                        return this.todos

                    // 筛选未完成和已完成的

                    // 定义一个数组，用于保存对应的todo
                    let tmp = []
                    for (let i = 0; i < this.todos.length; i++) {
                        if (this.todos[i].isFinish == this.Finish) {
                            tmp.push(this.todos[i]);
                        }
                    }
                    return tmp;
                },
            },

        }).mount('#app');

    </script>
</body>

</html>